package com.huayue.controller;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.huayue.annotation.LogAnno;
import com.huayue.entity.DepotOutOrder;
import com.huayue.entity.DepotOutOrderDetails;
import com.huayue.entity.User;
import com.huayue.service.DepotOutOrderService;
import com.huayue.util.DateHelper;
import com.huayue.util.OrderNumberHelper;
import com.huayue.util.PageHelper;

/**
 * 出库
 */
@Controller
@RequestMapping("/depotOutOrder")
public class DepotOutOrderController {
    @Autowired
    DepotOutOrderService svc;

    BaseController bc = new BaseController();

    @RequestMapping("/toAdd")
    public ModelAndView toAdd(DepotOutOrder depot, String type) {

        // 获取 审核人信息 商品信息 供货商信息
        FastJsonJsonView view = new FastJsonJsonView();
        // 根据前台判断是否需要生成订单号
        if ("true".equals(type)) {
            view.addStaticAttribute("orderNumber", OrderNumberHelper.getOrderNumber(OrderNumberHelper.CKDORDER, "CKD"));
        }

        view.addStaticAttribute("goodInfo", svc.findProduct());
        return new ModelAndView(view);
    }

    //
    /**
     * 清除订单号
     *
     * @param depotOutOrder
     * @return
     */
    @RequestMapping("/close")
    public ModelAndView close(DepotOutOrder depotOutOrder) {
        // 获取 审核人信息 商品信息 供货商信息
        FastJsonJsonView view = new FastJsonJsonView();
        OrderNumberHelper.removeOrderNumber(OrderNumberHelper.CKDORDER, depotOutOrder.getOrdernumber());
        view.addStaticAttribute("code", 0);
        return new ModelAndView(view);
    }

    //
    // @RequestMapping("/findYesDetails")
    // public ModelAndView findYesDetails(String id) {
    // FastJsonJsonView view = new FastJsonJsonView();
    // view.addStaticAttribute("code", 0);
    // DepotOutOrder depot = new DepotOutOrder();
    // depot.setId(Integer.valueOf(id));
    // view.addStaticAttribute("order", svc.findOne(depot));
    // view.addStaticAttribute("data", svc.findYesInDepotPurchaseDetails(depot));
    // return new ModelAndView(view);
    // }
    //
    @LogAnno(operateType = "添加-出库单")
    @RequestMapping("/add")
    public ModelAndView add(@RequestBody String json, HttpServletRequest request) {
        // 获取session中该用户信息
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("USER");
        // 格式化json数据
        JSONObject jsonData = JSONObject.parseObject(json);
        // 创建订单信息

        DepotOutOrder order = new DepotOutOrder();

        order.setEmployee_id_apply(user.getId());
        order.setOrdernumber(String.valueOf(jsonData.get("orderNumber")));
        order.setRemark(jsonData.get("remark").toString());
        JSONArray jsonArray = jsonData.getJSONArray("table");
        // 获取订单详情
        List<DepotOutOrderDetails> details = JSONArray.parseArray(jsonArray.toString(), DepotOutOrderDetails.class);
        // 将订单信息和订单详情传至service进行处理
        boolean result = svc.addDetails(order, details);
        // 将临时订单号销毁
        OrderNumberHelper.removeOrderNumber(OrderNumberHelper.CKDORDER, order.getOrdernumber());
        // 返回数据给页面
        FastJsonJsonView view = new FastJsonJsonView();
        view.addStaticAttribute("code", 0);
        if (result) {
            view.addStaticAttribute("icon", 1);
            view.addStaticAttribute("msg", "订单添加成功！");
        } else {
            view.addStaticAttribute("icon", 2);
            view.addStaticAttribute("msg", "订单添加失败！");
        }
        return new ModelAndView(view);
    }

    @LogAnno(operateType = "修改-出库单")
    @RequestMapping("/update")
    public ModelAndView update(DepotOutOrder depotOutOrder) {
        return bc.update(svc, depotOutOrder);
    }

    @LogAnno(operateType = "删除-出库单")
    @RequestMapping("/delete")
    public ModelAndView delete(DepotOutOrder depotOutOrder, HttpServletRequest request) {
        // 获取session中该用户信息
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("USER");
        depotOutOrder.setEmployee_id_ratify(user.getId());
        return bc.delete(svc, depotOutOrder);
    }

    @RequestMapping("/findOne")
    public ModelAndView findOne(DepotOutOrder depotOutOrder) {
        return bc.findOne(svc, depotOutOrder);
    }

    @RequestMapping("/findPage")
    public ModelAndView findPage(PageHelper page, DepotOutOrder depotOutOrder, String dateRange) {
        String begin = null;
        String end = null;
        if (dateRange != null && !"".equals(dateRange)) {
            begin = dateRange.split(" - ")[0];
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(DateHelper.StringToDateShort(dateRange.split(" - ")[1]));
            calendar.add(Calendar.DATE, 1);
            end = DateHelper.DateToStringShort(calendar.getTime());
        }
        return bc.findPage(svc, page, depotOutOrder, depotOutOrder, begin, end);
    }

    @RequestMapping("/findDetails")
    public ModelAndView findDetails(String id) {
        FastJsonJsonView view = new FastJsonJsonView();
        view.addStaticAttribute("code", 0);
        DepotOutOrder order = new DepotOutOrder();
        order.setId(Integer.valueOf(id));
        // 先找到一个
        view.addStaticAttribute("order", svc.findOne(order));
        // 然后传数据
        view.addStaticAttribute("data", svc.findDetails(id));
        return new ModelAndView(view);
    }

    @LogAnno(operateType = "审核-出库单")
    @RequestMapping("/check")
    public ModelAndView check(DepotOutOrder depotOutOrder, HttpServletRequest request) {
        // 获取session中该用户信息
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("USER");
        depotOutOrder.setEmployee_id_ratify(user.getId());
        FastJsonJsonView view = new FastJsonJsonView();
        view.addStaticAttribute("code", 0);

        List<Map<String, Object>> list = svc.findNumber(depotOutOrder);

        int a = 0;
        int b = 0;
        int c = 0;

        for (Map<String, Object> map : list) {
            a = Integer.valueOf((map.get("number")).toString());
            b = Integer.valueOf((map.get("real_number")).toString());
            c = b - a;
            int v = svc.UpdateReal_number(c, map.get("id").toString());
            a = 0;
            b = 0;
            c = 0;

            System.out.println(v);
        }
        int s = svc.check(depotOutOrder);

        if (s > 0) {
            view.addStaticAttribute("msg", "操作成功！");
            view.addStaticAttribute("icon", 1);
        } else {
            view.addStaticAttribute("msg", "操作失败！");
            view.addStaticAttribute("icon", 2);
        }
        return new ModelAndView(view);
    }

}
